/*
 * Copyright (c) 2011 Ondrej Lapacek
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

/**
 * @author      Ondrej Lapacek
 * @version     1.0, 09/12/11
 * @since       1.0
 */

package cz.muni.fi.android.assistivecamera.gallery;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
	
	int mGalleryItemBackground;
	private Context mContext; 
	private Uri[] mUrls; 

    public ImageAdapter(Context c, Uri[] mUrls) {
        mContext = c;
        this.mUrls = mUrls;
    }

    public int getCount() {
        return mUrls.length;
    }

    public Object getItem(int position) {
        return mUrls[position];
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    	ImageView imageView = (ImageView)convertView;
    	if(convertView == null) {
    		imageView = new ImageView(mContext);
    	}
    	
    	Bitmap bm = scaletoScreenSize(mUrls[position]);
    	
    	Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    	
    	int paddingWidth = (display.getWidth() - bm.getWidth())/2;
    	
        imageView.setImageBitmap(bm);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        
        if (display.getRotation() == 1 || display.getRotation() == 3) {
        	imageView.setLayoutParams(new Gallery.LayoutParams(display.getWidth(), display.getHeight()));
        	imageView.setPadding(paddingWidth, 0, paddingWidth, 0);
		} 
        if (display.getRotation() == 0 || display.getRotation() == 2) {
			imageView.setLayoutParams(new Gallery.LayoutParams(bm.getWidth(), bm.getHeight()));
			imageView.setPadding(0, 0, 0, 0);
		}
        
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
    
    public Bitmap scaletoScreenSize(Uri mUrls) {
    	
    	Bitmap bm = Bitmap.createBitmap(BitmapFactory.decodeFile(mUrls.getPath(), null));
    	Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    	
    	float currentWidth = bm.getWidth();
    	float currentHeight = bm.getHeight();
    	float displayWidth = display.getWidth();
    	float displayHeight = display.getHeight();

    	float ratio = currentHeight / currentWidth;

    	if (currentWidth > displayWidth && currentHeight > displayHeight) {
	        if (display.getRotation() == 0 || display.getRotation() == 2) { 
	        	// portrait
				if(ratio <= 1) { //height < width
					currentWidth = displayWidth;
					currentHeight = currentWidth * ratio;
				} else { //width < height
					currentWidth = displayWidth;
					currentHeight = currentWidth * ratio;
				}
	        }
	        if (display.getRotation() == 1 || display.getRotation() == 3) { 
	        	// landscape
				if(ratio <= 1) { //height < width
					currentHeight = displayHeight;
			    	currentWidth = currentHeight / ratio;
				} else { //width < height
					currentHeight = displayHeight;
					currentWidth = currentHeight / ratio;
				}
			}
	    	
	    	Bitmap bmp = Bitmap.createScaledBitmap(bm, (int)currentWidth, (int)currentHeight, false);
	    	return bmp;
    	}
    	
    	return bm;
    }
}